package com.caijin.web.controller.system.oss;

import com.caijin.common.core.domain.Result;
import com.caijin.system.service.IAliOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * AliOss 业务处理
 *
 * @author Peas
 */
@Api
@RestController
@CrossOrigin
public class AliOssController {

    @Autowired
    private IAliOssService aliOssService;

    /**
     * 上传用户头像
     */
    @ApiOperation(value = "上传用户头像")
    @PostMapping("/uploadImgFile/userAvatar/")
    public Result uploadImgFile(MultipartFile file
            , @RequestParam("type") String userType
            , @RequestParam("id") String userId) {
        aliOssService.addUserAvatarFileInfo(userType, userId);
        String s = aliOssService.upload(file);
        return Result.ok().data("url", s);
    }

    /**
     * 删除替换之后的头像
     */
    @ApiOperation(value = "删除替换之后的头像")
    @PostMapping("/deleteImgFile/userAvatar/")
    public Result deleteImgFile(@RequestParam("oldImg") String oldImg) {
        aliOssService.deleteFile(oldImg.substring(oldImg.indexOf(".com") + 5));
        return Result.ok();
    }

    /**
     * 上传课程封面
     */
    @ApiOperation(value = "上传课程封面")
    @PostMapping("/uploadImgFile/courseCover/")
    public Result uploadCourseCover(MultipartFile file) {
        aliOssService.addCourseCoverFileInfo();
        String upload = aliOssService.upload(file);
        return Result.ok().data("url", upload);
    }
}
